//
//  ZCSBoutiqueListenViewController.m
//  聆听FM
//
//  Created by dllo on 16/6/7.
//  Copyright © 2016年 Dark. All rights reserved.
//

#import "ZCSBoutiqueListenViewController.h"
#import "ZCSBoutListenModel.h"
#import "ZCSHearTableViewCell.h"
#import "ZCSReHeaderTableViewCell.h"

#import "ZCSListenListDetailViewController.h"

@interface ZCSBoutiqueListenViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) NSInteger maxPageId;
@property (nonatomic, retain) NSMutableArray *bigArray;
//数据源
@property (nonatomic, retain) NSMutableDictionary *dict;

@end

@implementation ZCSBoutiqueListenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"精品听单";
    [self creatView];
     self.dict = [NSMutableDictionary dictionary];
    self.bigArray = [NSMutableArray array];
    self.page = 1;
    [self getDataWithPage:self.page CaId:self.caId];
}

- (void)creatView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.sectionFooterHeight = 0;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCSHearTableViewCell" bundle:nil] forCellReuseIdentifier:@"hearTVC"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCSReHeaderTableViewCell" bundle:nil] forCellReuseIdentifier:@"reHeader"];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self getMoreData];
    }];

}
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
}
- (void)getMoreData
{
    self.page++;
    if (self.page > self.maxPageId) {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }
    else
    {

        [self getDataWithPage:self.page CaId:self.caId];
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return self.bigArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self.dict objectForKey:[self.bigArray objectAtIndex:section]] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZCSHearTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"hearTVC"];
    cell.backgroundColor = [UIColor whiteColor];
    cell.titleLabel.textColor = [UIColor blackColor];
    cell.numberLabel.textColor = [UIColor blackColor];
    cell.bouModel = [[self.dict objectForKey:[self.bigArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ZCSBoutListenModel *model = [[self.dict objectForKey:[self.bigArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
    ZCSListenListDetailViewController *ldvc = [[ZCSListenListDetailViewController alloc] init];
    ldvc.ID = model.specialId;
    ldvc.type = [NSString stringWithFormat:@"%ld", model.contentType];
    ldvc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:ldvc animated:YES];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100 * FITHEIGHT;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40 * FITHEIGHT;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    ZCSReHeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reHeader"];
    cell.backgroundColor = [UIColor whiteColor];
    cell.moreLabel.hidden = YES;
    cell.headerLabel.textColor = [UIColor blackColor];
    cell.headerLabel.text = [self.bigArray objectAtIndex:section];
    return cell;
}

- (void)getDataWithPage:(NSInteger)page CaId:(NSInteger)caId
{
    NSString *urlString ;
    if (caId) {
        urlString = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/subjects?categoryId=%ld&device=iPhone&page=%ld&per_page=20&scale=3", caId, page];
    }
    else
    {
        urlString = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/subject_list?device=android&page=%ld&per_page=10&statEvent=pageview%%2Fsubjectlist%%40%%E7%%B2%%BE%%E5%93%81%%E5%90%AC%%E5%%8D%%95&statModule=%%E7%%B2%%BE%%E5%93%81%%E5%90%AC%%E5%%8D%%95_%%E6%%9B%%B4%%E5%%A4%%9A&statPage=tab%40%E5%%8F%91%E7%%8E%%B0_%%E6%%8E%%A8%%E8%%8D%%90", page];
    }
    
    
    __weak ZCSBoutiqueListenViewController *blvc = self;
    [LTNetTool GetNetWithURL:urlString Body:nil Header:nil Response:0 Success:^(id result) {
        blvc.maxPageId = [result[@"maxPageId"] integerValue];
        NSArray *tempArray = result[@"list"];
        blvc.dataArray = [NSMutableArray array];
        for (NSDictionary *dic in tempArray) {
            ZCSBoutListenModel *model = [[ZCSBoutListenModel alloc] initWithDic:dic];
            [blvc.dataArray addObject:model];
        }
        for (ZCSBoutListenModel *bouModel in blvc.dataArray) {
            NSString*str= [NSString stringWithFormat:@"%lld",bouModel.releasedAt];//时间戳
            
            NSTimeInterval time=[str doubleValue]/1000;
            
            NSDate*detaildate=[NSDate dateWithTimeIntervalSince1970:time];
            
            //实例化一个NSDateFormatter对象
            
            NSDateFormatter*dateFormatter = [[NSDateFormatter alloc]init];
            
            //设定时间格式,这里可以设置成自己需要的格式
            
            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            
            NSString*currentDateStr = [dateFormatter stringFromDate:detaildate];
            //  NSLog(@"%@", currentDateStr);
            
            NSString *tempString = [currentDateStr substringWithRange:NSMakeRange(0, 7)];
          
            if ([blvc.dict.allKeys containsObject:tempString]) {
                NSMutableArray *array = [blvc.dict objectForKey:tempString];
                [array addObject:bouModel];
                [blvc.dict setValue:array forKey:tempString];
            }
            else
            {
                [blvc.bigArray addObject:tempString];
                NSMutableArray *array = [NSMutableArray array];
                [array addObject:bouModel];
                [blvc.dict setValue:array forKey:tempString];
            }
           
        }
        
        
        [blvc.tableView reloadData];
        [blvc.tableView.mj_footer endRefreshing];
        
    } Failure:^(NSError *error) {
        NSLog(@"%@", error);
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
